package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportInvoice;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportInvoiceDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-07 10:50:10
 */
public class TransportInvoiceConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportInvoiceConverts.class);

    public static List<TransportInvoiceDTO> convert(List<TransportInvoice> transportInvoiceList) {
        if (CollectionUtils.isEmpty(transportInvoiceList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportInvoiceDTO> transportInvoiceListResult = Lists.newArrayList();
        for (TransportInvoice transportInvoice : transportInvoiceList) {
            transportInvoiceListResult.add(convert(transportInvoice));
        }
        return transportInvoiceListResult;
    }

    public static TransportInvoiceDTO convert(TransportInvoice transportInvoice) {
        try {
            return BeanUtils.copy(transportInvoice, TransportInvoiceDTO.class);
        } catch (Exception e) {
            LOG.error("TransportInvoice到TransportInvoiceDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportInvoice到TransportInvoiceDTO转换失败", e);
        }
    }

    public static TransportInvoice convert(TransportInvoiceDTO transportInvoiceDTO) {
        try {
            return BeanUtils.copy(transportInvoiceDTO, TransportInvoice.class);
        } catch (Exception e) {
            LOG.error("TransportInvoiceDTO到TransportInvoice转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportInvoiceDTO到TransportInvoice转换失败", e);
        }
    }
}